﻿using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using YiSah.Entity.Extension.User;
using YiSha.Admin.Web.Controllers;
using YiSha.Basics;
using YiSha.Entity.OrganizationManage;
using YiSha.Enums;
using YiSha.Framework;
using YiSha.Model.Param;
using YiSha.Model.Result;
using YiSha.Model.Result.SystemManage;
using YiSha.Service.OrganizationManage;
using YiSha.Service.SystemManage;
using YiSha.Util;
using YiSha.Util.Model;

namespace YiSha.Admin.Web.Areas.OrganizationManage.Controllers
{
    [Area("OrganizationManage")]
    public class UserController : BaseController
    {
        /// <summary>
        /// 用户服务类
        /// </summary>
        public UserService userService { get; set; }
        public MenuAuthorizeService menuAuthorizeService { get; set; }
        //private DepartmentBLL departmentBLL = new DepartmentBLL();

        #region 视图功能
        [AuthorizeFilter("organization:user:view")]
        public IActionResult UserIndex()
        {
            return View();
        }

        public IActionResult UserForm()
        {
            return View();
        }

        public IActionResult UserDetail()
        {
            ViewBag.Ip = NetHelper.Ip;
            return View();
        }

        public IActionResult ResetPassword()
        {
            return View();
        }

        public async Task<IActionResult> ChangePassword()
        {
            ViewBag.OperatorInfo = await OperatorService.Instance.Current();
            return View();
        }

        public IActionResult ChangeUser()
        {
            return View();
        }

        public async Task<IActionResult> UserPortrait()
        {
            ViewBag.OperatorInfo = await OperatorService.Instance.Current();
            return View();
        }

        public IActionResult UserImport()
        {
            return View();
        }
        #endregion

        #region 获取数据
        [HttpGet]
        [AuthorizeFilter("organization:user:search")]
        public async Task<IActionResult> GetListJson(UserEntity param)
        {
            TData<List<UserEntity>> obj = await userService.GetList(param);
            return Json(obj);
        }

        [AuthorizeFilter("organization:user:search")]
        public async Task<IActionResult> GetPageListJson(UserReq param)
        {
            var obj = await userService.GetPageList(param);
            return Json(obj);
        }

        [HttpGet]
        public async Task<IActionResult> GetFormJson(string id)
        {
            TData<UserEntity> obj = await userService.GetUser(id);
            return Json(obj);
        }

        [HttpGet]
        public async Task<IActionResult> GetUserAuthorizeJson()
        {
            TData<UserAuthorizeInfo> obj = new TData<UserAuthorizeInfo>();
            OperatorInfo operatorInfo = await OperatorService.Instance.Current();
            TData<List<MenuAuthorizeInfo>> objMenuAuthorizeInfo = await menuAuthorizeService.GetAuthorizeList(operatorInfo);
            obj.Data = new UserAuthorizeInfo();
            obj.Data.IsSystem = operatorInfo.IsSystem;
            if (objMenuAuthorizeInfo.MsgCode == MsgCode.Success)
            {
                obj.Data.MenuAuthorize = objMenuAuthorizeInfo.Data;
            }
            obj.MsgCode = MsgCode.Success;
            return Json(obj);
        }
        #endregion

        #region 提交数据
        /// <summary>
        /// 新增/修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        [AuthorizeFilter("organization:user:add,organization:user:edit")]
        public async Task<IActionResult> SaveFormJson(UserEntityExtension entity)
        {
            if (!ModelState.IsValid)
            {
                var msg = ModelState.Values.Where(w => w.Errors.Count > 0).Select(s => s.Errors).FirstOrDefault()[0].ErrorMessage;
                return Json(msg);
            }
            TData obj = await userService.SaveForm(entity);
            return Json(obj);
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        [AuthorizeFilter("organization:user:delete")]
        public async Task<IActionResult> DeleteFormJson(List<string> ids)
        {
            TData obj = await userService.DeleteForm(ids);
            return Json(obj);
        }

        //[HttpPost]
        //[AuthorizeFilter("organization:user:resetpassword")]
        //public async Task<IActionResult> ResetPasswordJson(UserEntity entity)
        //{
        //    TData<long> obj = await userBLL.ResetPassword(entity);
        //    return Json(obj);
        //}

        // [HttpPost]
        //public async Task<IActionResult> ChangePasswordJson(ChangePasswordParam entity)
        //{
        //    TData<long> obj = await userBLL.ChangePassword(entity);
        //    return Json(obj);
        //}

        //[HttpPost]
        //public async Task<IActionResult> ChangeUserJson(UserEntity entity)
        //{
        //    TData<long> obj = await userBLL.ChangeUser(entity);
        //    return Json(obj);
        //}

        [HttpPost]
        public async Task<IActionResult> ImportUserJson(ImportParam param)
        {
            List<UserEntity> list = new ExcelHelper<UserEntity>().ImportFromExcel(param.FilePath);
            // TData obj = await userBLL.ImportUser(param, list);
            return Json(null);
        }

        [HttpPost]
        public async Task<IActionResult> ExportUserJson(UserEntity param)
        {
            TData<string> obj = new TData<string>();
            TData<List<UserEntity>> userObj = await userService.GetList(param);
            if (obj.MsgCode == MsgCode.Success)
            {
                string file = new ExcelHelper<UserEntity>().ExportToExcel("用户列表.xls",
                                                                          "用户列表",
                                                                          userObj.Data,
                                                                          new string[] { "UserName", "RealName", "Gender", "Mobile", "Email" });
                obj.Data = file;
                obj.MsgCode = MsgCode.Success;
            }
            return Json(obj);
        }
        #endregion
    }
}